﻿<template>
  <div class="l-rblock l-form-viewer" style="padding: 8px">
    <el-form
      :model="formData"
      :rules="rules"
      size="mini"
      labelPosition="right"
      labelWidth="95px"
      ref="form"
    >
      <div class="l-auto-window only-tabs">
        <el-tabs v-model="activeName" type="card">
          <el-tab-pane
            class="l-tabs-container"
            :label="$t('主表信息')"
            name="tab0"
          >
            <el-row :gutter="0">
              <div class="l-rblock">
                <el-col
                  v-if="lr_formLookAuth('case_erp_invoicelog_f_Number')"
                  :span="12"
                >
                  <el-form-item
                    :label="$t('开票编号')"
                    prop="case_erp_invoicelog_f_Number"
                  >
                    <l-code
                      size="mini"
                      code="erp_Invoicelog_number"
                      v-model="formData.case_erp_invoicelog_f_Number"
                    />
                  </el-form-item>
                </el-col>
                <el-col
                  v-if="lr_formLookAuth('case_erp_invoicelog_f_Theme')"
                  :span="12"
                >
                  <el-form-item
                    :label="$t('开票主题')"
                    prop="case_erp_invoicelog_f_Theme"
                  >
                    <el-input
                      :disabled="
                        !lr_formEditAuth(
                          'case_erp_invoicelog_f_Theme',
                          isReadOnly
                        )
                      "
                      v-model="formData.case_erp_invoicelog_f_Theme"
                      :placeholder="$t('请输入')"
                    >
                    </el-input>
                  </el-form-item>
                </el-col>
                <el-col
                  v-if="lr_formLookAuth('case_erp_invoicelog_f_Date')"
                  :span="12"
                >
                  <el-form-item
                    :label="$t('开票日期')"
                    prop="case_erp_invoicelog_f_Date"
                  >
                    <l-date
                      :disabled="
                        !lr_formEditAuth(
                          'case_erp_invoicelog_f_Date',
                          isReadOnly
                        )
                      "
                      :readonly="false"
                      format="yyyy-MM-dd"
                      dateType="date"
                      :clearable="true"
                      v-model="formData.case_erp_invoicelog_f_Date"
                      :placeholder="$t('请选择')"
                    />
                  </el-form-item>
                </el-col>
                <el-col
                  v-if="lr_formLookAuth('case_erp_invoicelog_f_InvoiceName')"
                  :span="12"
                >
                  <el-form-item
                    :label="$t('发票类型')"
                    prop="case_erp_invoicelog_f_InvoiceName"
                  >
                    <l-select
                      :disabled="
                        !lr_formEditAuth(
                          'case_erp_invoicelog_f_InvoiceName',
                          false || isReadOnly
                        )
                      "
                      :options="case_erp_invoicelog_f_InvoiceNameOptions"
                      v-model="formData.case_erp_invoicelog_f_InvoiceName"
                    />
                  </el-form-item>
                </el-col>
                <el-col
                  v-if="lr_formLookAuth('case_erp_invoicelog_f_TicketNum')"
                  :span="12"
                >
                  <el-form-item
                    :label="$t('发票号码')"
                    prop="case_erp_invoicelog_f_TicketNum"
                  >
                    <el-input
                      :disabled="
                        !lr_formEditAuth(
                          'case_erp_invoicelog_f_TicketNum',
                          isReadOnly
                        )
                      "
                      :readonly="false"
                      v-model="formData.case_erp_invoicelog_f_TicketNum"
                      :placeholder="$t('请输入')"
                    >
                    </el-input>
                  </el-form-item>
                </el-col>
                <el-col
                  v-if="lr_formLookAuth('case_erp_invoicelog_f_TicketAmount')"
                  :span="12"
                >
                  <el-form-item
                    :label="$t('发票金额')"
                    prop="case_erp_invoicelog_f_TicketAmount"
                  >
                    <el-input
                      :disabled="
                        !lr_formEditAuth(
                          'case_erp_invoicelog_f_TicketAmount',
                          isReadOnly
                        )
                      "
                      v-model="formData.case_erp_invoicelog_f_TicketAmount"
                      :placeholder="$t('请输入')"
                    >
                    </el-input>
                  </el-form-item>
                </el-col>
                <el-col
                  v-if="lr_formLookAuth('case_erp_invoicelog_f_TicketName')"
                  :span="24"
                >
                  <el-form-item
                    :label="$t('收票方')"
                    prop="case_erp_invoicelog_f_TicketName"
                  >
                    <el-input
                      :disabled="
                        !lr_formEditAuth(
                          'case_erp_invoicelog_f_TicketName',
                          isReadOnly
                        )
                      "
                      v-model="formData.case_erp_invoicelog_f_TicketName"
                      :placeholder="$t('请输入')"
                    >
                    </el-input>
                  </el-form-item>
                </el-col>
                <el-col
                  v-if="lr_formLookAuth('case_erp_invoicelog_f_SaleId')"
                  :span="24"
                >
                  <el-form-item
                    :label="$t('销售订单')"
                    prop="case_erp_invoicelog_f_SaleId"
                  >
                    <l-layer-select
                      :disabled="
                        !lr_formEditAuth(
                          'case_erp_invoicelog_f_SaleId',
                          false || isReadOnly
                        )
                      "
                      :isPage="true"
                      :height="504"
                      :width="960"
                      :columns="[
                        {
                          label: '销售单号',
                          prop: 'f_number',
                          valueKey: '',
                          minWidth: '120',
                          align: 'left',
                          hidden: false,
                        },
                        {
                          label: '订单主题',
                          prop: 'f_theme',
                          valueKey: '',
                          minWidth: '150',
                          align: 'left',
                          hidden: false,
                        },
                        {
                          label: '采购日期',
                          prop: 'f_saledate',
                          valueKey: '',
                          minWidth: '150',
                          align: 'left',
                          hidden: false,
                        },
                        {
                          label: '客户名称',
                          prop: 'f_clientname',
                          valueKey: '',
                          minWidth: '120',
                          align: 'left',
                          hidden: false,
                          dataType: 'dataSource',
                          dataCode: 'ERP_Customers',
                          valueKey: 'f_id',
                          labelKey: 'f_name',
                          isNotAutoWrap: true,
                        },
                        {
                          label: '联系人',
                          prop: 'f_clientperson',
                          valueKey: '',
                          minWidth: '120',
                          align: 'left',
                          hidden: false,
                        },
                        {
                          label: '联系方式',
                          prop: 'f_clientway',
                          valueKey: '',
                          minWidth: '120',
                          align: 'left',
                          hidden: false,
                        },
                      ]"
                      :options="case_erp_invoicelog_f_SaleIdOptions"
                      v-model="formData.case_erp_invoicelog_f_SaleId"
                    />
                  </el-form-item>
                </el-col>
                <el-col
                  v-if="lr_formLookAuth('case_erp_invoicelog_f_Description')"
                  :span="24"
                >
                  <el-form-item
                    :label="$t('备注')"
                    prop="case_erp_invoicelog_f_Description"
                  >
                    <el-input
                      type="textarea"
                      :disabled="
                        !lr_formEditAuth(
                          'case_erp_invoicelog_f_Description',
                          isReadOnly
                        )
                      "
                      :rows="10"
                      v-model="formData.case_erp_invoicelog_f_Description"
                      :placeholder="$t('请输入')"
                    />
                  </el-form-item>
                </el-col>
              </div>
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </div>
    </el-form>
  </div>
</template>
<script>
const api = window.$api.erpcase.invoicelog;

export default {
  props: {
    isReadOnly: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      moduleCode: "ERP_InvoiceLogs",
      activeName: "tab0",
      formData: {
        case_erp_invoicelog_f_Number: "", // 开票编号
        case_erp_invoicelog_f_Theme: "", // 开票主题
        case_erp_invoicelog_f_Date: "", // 开票日期
        case_erp_invoicelog_f_InvoiceName: "", // 发票类型
        case_erp_invoicelog_f_TicketNum: "", // 发票号码
        case_erp_invoicelog_f_TicketAmount: "", // 发票金额
        case_erp_invoicelog_f_TicketName: "", // 收票方
        case_erp_invoicelog_f_SaleId: "", // 销售订单
        case_erp_invoicelog_f_Description: "", // 备注
      },
      rules: {
        case_erp_invoicelog_f_Theme: [
          { required: true, message: "请输入开票主题" },
        ],
        case_erp_invoicelog_f_Date: [
          { required: true, message: "请选择开票日期" },
        ],
        case_erp_invoicelog_f_InvoiceName: [
          { required: true, message: "请选择发票类型" },
        ],
        case_erp_invoicelog_f_TicketNum: [
          { required: true, message: "请输入发票号码" },
          { pattern: /^[0-9a-zA-Z_]{1,}$/, message: "请输入正确发票号码" },
        ],
        case_erp_invoicelog_f_TicketAmount: [
          { required: true, message: "请输入发票金额" },
          { pattern: /^\d+$|^\d*\.\d+$/, message: "请输入数字" },
        ],
        case_erp_invoicelog_f_TicketName: [
          { required: true, message: "请输入收票方" },
        ],
        case_erp_invoicelog_f_SaleId: [
          { required: true, message: "请选择销售订单" },
        ],
      },
    };
  },
  computed: {
    case_erp_invoicelog_f_InvoiceNameOptions() {
      return [
        { value: "增值税专用发票", label: "增值税专用发票" },
        { value: "普通发票", label: "普通发票" },
      ];
    },
    case_erp_invoicelog_f_SaleIdOptions() {
      const data = this.lr_DataSource("ERP_SaleList");
      return this.lr_DataSourceOptions(data, "f_id", "f_theme");
    },
  },
  methods: {
    // 重置表单
    resetForm() {
      this.$formClear(this.$refs.form);
      this.activeName = "tab0";
    },
    // 校验表单
    async validateForm() {
      if (!(await this.$formValidateWraper(this.$refs.form))) {
        return false;
      }

      return true;
    },
    // 设置表单数据
    setForm(data) {
      this.$setFormData(this.formData, data, "case_erp_invoicelog");
    },
    // 获取表单数据
    getForm() {
      return this.$getFormData(this.formData, "case_erp_invoicelog");
    },

    async loadFormData({ keyValue /*,params,node*/ }) {
      //  keyValue 流程中相当于流程processId,params 传递参数,node 流程节点
      const data = await this.$awaitWraper(api.get(keyValue));
      if (data) {
        this.setForm(data);
        return true;
      } else {
        return false;
      }
    },

    async saveForm({ keyValue, isEdit /*,code,node*/ }) {
      // isEdit 是否更新数据, keyValue 流程中相当于流程processId,code 表示流程中的操作码,node 流程节点
      const postData = this.getForm();
      if (isEdit) {
        // 编辑
        return await this.$awaitWraper(api.update(keyValue, postData));
      } else {
        // 新增
        postData.f_Id = keyValue;

        return await this.$awaitWraper(api.add(postData));
      }
    },
  },
};
</script>